/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * $Id: timestamp.hpp,v 1.7 2009-05-13 07:18:19 amaula Exp $
 *
 * \file
 * \brief Definition for common datatype 'Timestamp'
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _MACI_COMMON_TIMESTAMP_HPP_
#define _MACI_COMMON_TIMESTAMP_HPP_
#include "ownutils.h"
#include "gimutils.h"

namespace MaCI {
  namespace Common {

    /** Timestamp.
     * 
     * This structure defines a MaCI common format for transferring
     * timestamps. All MaCI interfaces requiring timestamp dataformat
     * should use this to avoid multiple parallel implementations.
     */
    struct TTimestamp 
    {
      /** Default constructor. (Sets time to current time)
       */
      TTimestamp() 
        : timestamp_s(0),
          timestamp_us(0) {
        SetToCurrentTime();
      }
      
      /** Constructor to directly set time.
       * 
       * @param[in] aS          Seconds part of timestamp.
       * @param[in] aUS         Microseconds part of timestamp.
       */
      TTimestamp(const unsigned int aS, const unsigned int aUS) 
        : timestamp_s(aS),
          timestamp_us(aUS) {}


      /** Constructor to set time from gim::time container.
       * 
       * @param[in] aGimTime    gim::time object.
       */
      TTimestamp(const gim::time &aGimTime)
        : timestamp_s(aGimTime.getSeconds()),
          timestamp_us(aGimTime.getUSeconds()) {}


      /** Function to set the time from gim::time.
       *
       * @param[in] aTime gim::time object to set the time from.
       */
      void SetTime(const gim::time &aTime) {
        timestamp_s = aTime.getSeconds();
        timestamp_us = aTime.getUSeconds();
      }


      /** Return time as gim::time object.
       * @return                New instance of gim::tmie containing the time.
       */
      gim::time GetGimTime(void) const {
        return gim::time(static_cast<long int>(timestamp_s), static_cast<long int>(timestamp_us));
      }

      
      /** Set timestamp to current system time.
       *
       * Calling this function sets the timestamp to current time.
       */
      void SetToCurrentTime(void) {
        long int s,us;
        ownTime_GetTimeOfDay(&s,&us);
        timestamp_s = s;
        timestamp_us = us;
      }

      uint32_t timestamp_s;   ///< Seconds part of timestamp
      uint32_t timestamp_us;  ///< Microseconds part of timestamp
    };
  }
}

#endif
